#include "STrainer.h"
#include "ICheat.h"
#include "resource.h"

namespace TLib
{	

	enum MAIN_WINDOW_BUTTON_IDS
	{
		MWBI_ABOUT,
		MWBI_CHEATS
	};


	TRAINER_LIB_API LRESULT CALLBACK STrainer::tr_wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
	{
		static STrainer* app;
		static HWND bAbout, bCheats;
		static HINSTANCE hInstance;
		switch(msg)
		{
		case WM_CREATE:
			{
				app = ((STrainer*)(((CREATESTRUCT*)lParam)->lpCreateParams));
				unsigned int tr_cyWnd,tr_cxWnd, bHSep, bH, bW, bX, bExtra;
				tr_cyWnd = app->getTrainerHeight();
				tr_cxWnd = app->getTrainerWidth();

				hInstance = app->getTrainerHInstance();

				bExtra = 5;
				bHSep = tr_cyWnd/4;
				bW = 50 + bExtra;
				bX = tr_cxWnd/2 - (bW/2);
				bH = 20 + bExtra;

				bCheats = CreateWindowA
					(
					TEXT("button"), 
					"Cheats", 
					WS_VISIBLE | WS_CHILD, 
					bX,
					bHSep, 
					bW,
					bH,
					hWnd, 
					(HMENU)MWBI_CHEATS, 
					(HINSTANCE)hInstance, 
					0
					);

				bAbout = CreateWindowA
					(
					TEXT("button"), 
					"About", 
					WS_VISIBLE | WS_CHILD, 
					bX,
					bHSep*2, 
					bW, 
					bH,
					hWnd, 
					(HMENU)MWBI_ABOUT, 
					(HINSTANCE)hInstance, 
					0
					);

				SetTimer(hWnd, 1, 1000, NULL);
				return 0;
			}
		case WM_HOTKEY:
			{
				int id = static_cast<int>(wParam);
				
				HANDLE hProcess = app->GetGameProcessHandle();
				if (hProcess == NULL)
					return 0;
				
				if (app->getCheat(id)->isEnabled())
					app->getCheat(id)->Off(hProcess);
				else
					app->getCheat(id)->On(hProcess);
				
				CloseHandle(hProcess);
				return 0;
			}
		case WM_TIMER:
			{
				if (FindWindow(NULL, app->getGameTitle().c_str()) == NULL)
				{
					for (unsigned int i = 0; i < app->getNumberOfCheats(); i++)
					{
						app->getCheat(i)->gameTurnedOff();
					}
				}
				return 0;
			}
		case SET_BUTTON_GEOMETRY:
			{
				GEOMETRY_PARAMS* geom = (GEOMETRY_PARAMS*)wParam;
				BUTTON_TYPE* bt = (BUTTON_TYPE*)lParam;
				HWND b;
				if (*bt == BT_ABOUT)
					b = bAbout;
				else if (*bt == BT_CHEATS)
					b = bCheats;
				MoveWindow(b, geom->x, geom->y, geom->w, geom->h, TRUE);
				return 0;
			}
		case WM_COMMAND:
			{
				// The ID of the control is the low word of wParam.
				unsigned int id = LOWORD(wParam);

				if (id < 0 || id > 1)
					return 0;

				switch (id)
				{
				case MWBI_CHEATS:
					DialogBoxParam((HINSTANCE)dllHModule, 
						MAKEINTRESOURCE(IDD_CHEAT), 
						app->getTrainerHWnd(), 
						tr_cheatDlgProc,
						(LPARAM)app);
					break;
				case MWBI_ABOUT:
					MessageBoxA(app->getTrainerHWnd(),
						app->getAboutInfo().c_str(),
						"About",
						NULL);
					break;
				}

				return 0;
			}
		case WM_DESTROY:
			{
				// Close the application entirely
				PostQuitMessage(0);
				return 0;
			}
		default:
			{
				break;
			}
		}

		// Handle any messages the switch statement didn't
		return DefWindowProc (hWnd, msg, wParam, lParam);
	}


}// TLib Namespace